package capitulo7;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Random;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class AnimationTest extends JFrame implements Runnable {
	static int screenWidth=640;
	static int screenHeight=480;
	Thread gameLoop;
	Random rand = new Random(); 

	// Double buffer objects
	BufferedImage backBuffer;
	Graphics2D g2d;
	
	// Sprite variables
	Image image;
	Point pos = new Point(300,200);
	
	// Animation variables
	int currentFrame=0;
	int totalFrames=30;
	int animationDirection = 1;
	int frameCount=0;
	int frameDelay=0;
	
	public static void main(String[] args) {
		new AnimationTest();
	}

	public AnimationTest(){
		super("Animation test");
		setSize(screenWidth, screenHeight);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// create the back buffer for smooth graphics
		backBuffer = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB);
		g2d = backBuffer.createGraphics();
		
		// load the ball animation strip
		Toolkit tk = Toolkit.getDefaultToolkit();
		image = tk.getImage(getURL("explosion.png"));
		
		gameLoop = new Thread(this);
		gameLoop.start();	
	}
	
	private URL getURL(String filename){
		URL url = null;
		try {
			url = this.getClass().getResource(filename);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return url;
	}
	
	@Override
	public void run() {
		Thread t = Thread.currentThread();
		while (t==gameLoop){
			try {
				Thread.sleep(5);
			} catch (Exception e) {}
			gameUpdate();	
		}
	}
	
	public void gameUpdate(){
		// Clear the background
		g2d.setColor(Color.BLACK);
		g2d.fillRect(0, 0, screenWidth, screenHeight);
		
		// draw the current frame of animation
		drawFrame(image, g2d, pos.x, pos.y, 6, currentFrame, 128,128);
		
		g2d.setColor(Color.WHITE);
		g2d.drawString("Position: " + pos.x + "," + pos.y, 10, 50);
		g2d.drawString("Animation: " + currentFrame, 10, 70);
		
		// See if it's time to animate
		frameCount++;
		if (frameCount > frameDelay){
			frameCount = 0;
			// update the animation frame
			currentFrame+=animationDirection;
			if (currentFrame > totalFrames -1){
				currentFrame = 0;
				pos.x = rand.nextInt(screenWidth-128);
				pos.y = rand.nextInt(screenHeight-128);
			} else if (currentFrame < 0){
				currentFrame = totalFrames-1;
			}
		}
		repaint();
	}
	
	public void paint(Graphics g){
		g.drawImage(backBuffer, 0, 0, this);
	}

	// Draw a single frame of animation
	public void drawFrame(Image source, Graphics2D dest, int x, int y, int cols, int frame, int width, int height){
		int fx = (frame%cols)*width;
		int fy = (frame/cols)/height;
		dest.drawImage(source, x, y, x+width, y+height, fx, fy, fx+width, fy+height, this);
	}
}
